import { getUserInfoApi } from '@/api/user/index'

const user = {
  namespaced: true,
  state: {
    userInfo: {},
    loginStorage: {}
  },
  getters: {
  },
  mutations: {
    addUserInfo: (state, obj) => {
      state.userInfo = obj
    },
    addLoginSrotage: (state, obj) => {
      state.loginStorage = obj
      for (const key in obj) {
        if (Object.hasOwnProperty.call(obj, key)) {
          const element = obj[key]
          sessionStorage.setItem(key, JSON.stringify(element))
        }
      }
    }
  },
  actions: {
    getUserInfo({ commit, state }, params) {
      return new Promise((resolve, reject) => {
        getUserInfoApi(params).then(res => {
          if (res.code === 1) {
            let userInfoData = res.data
            // delete userInfoData.deleted
            commit('addUserInfo', res.data)
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
